const express=require("express");
const db=require("../db/index");
const Users= require("../models/data");
const bodyParser = require('body-parser');
const router=new express.Router();

router.use(bodyParser.urlencoded({
    extended: true
}));

router.use('/', async (req, res, next) => {
    await db;
    next();
})

router.post("/login", async(req,res)=>{
    console.log(req.body);
    const {
        user_name,
        password
    }=req.body;

    const user_name_reg=/^[A-z0-9]{6,12}/;
    const password_reg=/^[A-z0-9]{6,12}/;

    if(user_name_reg.test(user_name.trim()) && password_reg.test(password.trim())){
        const result_find=await Users.findOne({
            user_name
        });
        
        console.log(result_find);
        if(result_find){
            res.send('验证错误，请重新输入！');
        }else{
            const result =await Users.create({
                user_name,
                password
            })
            console.log(result);
        }
        res.send('登录成功!');
    }else{
        res.send("数据不匹配，请重新输入！！")
    }

})

module.exports= router;